//Hou
app.controller('addLinePreOrderController', ['$scope', 'lineCustomService', 'commodityService','ajax','ajax2', '$state', '$stateParams', 'toolService','$filter','Flash','uploadService',  function ($scope, lineCustomService, commodityService, ajax,ajax2,$state, $stateParams, toolService,$filter,Flash,uploadService) {


    //初始化商品类目
    $scope.f_add = {
        name : ""
    };
    $scope.s_add = {
        name : ""
    }
//添加商品分类

    ajax.get({
        url: 'back/commodityCategory/list',
        data:{
            level:1
        },
        debug: false
    }).then(function (data) {


        $scope.f_add_class=data;

        $scope.$watch('f_add.name', function(data) {
            if($scope.f_add){
                ajax.get({
                    url: 'back/commodityCategory/list',
                    data:{
                        parentId:$scope.f_add.name.categoryId
                    },
                    debug: false
                }).then(function (data) {
                    $scope.s_add_class=data;

                    //加载默认值
                    if($scope.data.defaultCategoryId){

                        for(var i=0;i<$scope.s_add_class.length;i++){

                            if($scope.s_add_class[i].categoryId == $scope.data.defaultCategoryId){
                                $scope.s_add.name = $scope.s_add_class[i];
                            }

                        }

                    }
                })
            }
        });
    })
    $scope.data = {
        isShowAddView : false,
        isShowUpdate: false,
        popViewTitle:"添加商品",
        isShowbtn:false,
        isAdd : true
    };
    $scope.showAddView = function(){
        $scope.f_add = {
            name : ""
        };
        $scope.s_add = {
            name : ""
        }
        $scope.data.popViewTitle = "添加商品";
        $scope.data.isAdd = "true";
        $scope.data.isShowAddView = true;
        $scope.customer=null;
    };
    $scope.commodityDetail = function(data){

        data.categoryId=$scope.s_add.name.categoryId;
        data.imageId = $scope.imageId;
        data.imageUrl  = $scope.imageUrl;
        console.log(data);
        //判断是添加还是修改
        if($scope.data.isAdd){
            ajax.post({
                url: 'back/commodity/add',
                data: data,
                debug: true
            }).then(function(){
                Flash.create('success', "保存成功", 'custom-class');
                $scope.data.isShowAddView =false;
                list($scope.page.currentPage);
                $state.go('admin.commodity',null,{
                    reload:true
                });
            })
        }else{
            ajax.post({
                url: 'back/commodity/update',
                data: data,
                debug: true
            }).then(function(){
                Flash.create('success', "修改成功", 'custom-class');
                $scope.data.isShowAddView =false;
                list($scope.page.currentPage);
                $state.go('admin.commodity',null,{
                    reload:true
                });
            })

        }

    };
    //上传图片
    $scope.uploadPic = function(data){
        uploadService.upload(data).then(function(result){
            $scope.imageUrl = result.url;
            $scope.imageId = result.attachmentId;
        });
    }

    //获取明天的日期
    var getDate = new Date().getDate();
    var setDate=getDate+1;
    //3.0.toExponential();
    var getFullYear = new Date().getFullYear();
    var getMonth = new Date().getMonth() + 1;
    $scope.day = getFullYear + '-' + getMonth + '-' + setDate;


    //判断是更新数据还是添加数据
    if ($stateParams.id) {
        ajax.get({
            url: 'back/order/query',
            data: {
                orderId: $stateParams.id
            }
        }).then(function (result) {
            $scope.detail = result;
            ajax.get({
                url: 'back/order/orderDetailList',
                data: {
                    orderId: $stateParams.id
                },
                debug: true
            }).then(function (result) {

                $scope.detail.commoditys = result;
                result.forEach(function(item,index){
                    $scope.detail.commoditys[index].outPrice = item.unitPrice;
                });
            })
        })
    } else {
        $scope.commoditys = []
    }



    // 客户列表分页查询
    function lineCustomList(page) {
        lineCustomService.list({
            currentPage: page,
            pageSize: 10
        }).then(function (result) {
            $scope.linCustomerList = result;
        });
    }

    lineCustomList(1);
    //分页
    $scope.linCustomerPageChanged = function (e) {
        lineCustomList(e);
    };


    // 商品列表
    commodityService.list().then(function (result) {
        $scope.productList = result;
    });


    /*
     * 移出商品
     */
    $scope.removeCart = function (id) {
        $scope.detail.commoditys.forEach(function (item, index, array) {
            if (item.commodityId === id) {
                array.splice(index, 1);
            }
        })
    }

    //生成订单
    $scope.submit = function () {

        var ctime_tstart=$filter('date')($scope.detail.receivingDate,'yyyy-MM-dd');
        $scope.detail.receivingDate=ctime_tstart;

        var submitData = angular.copy($scope.detail);
        submitData.commoditys.forEach(function(item,index,arr){
            arr[index] = JSON.stringify(item)
        });
        if ($scope.detail.orderId) {
            var url = 'back/order/update'
        } else {
            var url = 'back/order/add';
        };
        ajax.post({
            data: submitData,
            url: url,
            debug: true,

        }).then(function (result) {
            $state.go('admin.linePreOrder')
        })
    }

}]);